﻿// Decompiled with JetBrains decompiler
// Type: MCM.Abstractions.Base.PerSave.FluentPerSaveSettings
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection;
using MCM.Abstractions.FluentBuilder;
using MCM.Abstractions.PerSave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

#nullable enable
namespace MCM.Abstractions.Base.PerSave
{
  public class FluentPerSaveSettings : PerSaveSettings, IFluentSettings
  {
    public override sealed string Id { get; }

    public override sealed string DisplayName { get; }

    public override sealed string FolderName { get; }

    public override sealed string SubFolder { get; }

    public override string DiscoveryType => "fluent";

    public override sealed int UIVersion { get; }

    public override sealed char SubGroupDelimiter { get; }

    public override sealed event PropertyChangedEventHandler? PropertyChanged
    {
      add => base.PropertyChanged += value;
      remove => base.PropertyChanged -= value;
    }

    public List<SettingsPropertyGroupDefinition> SettingPropertyGroups { get; }

    private List<ISettingsPresetBuilder> Presets { get; }

    public FluentPerSaveSettings(
      string id,
      string displayName,
      string folderName,
      string subFolder,
      int uiVersion,
      char subGroupDelimiter,
      PropertyChangedEventHandler? onPropertyChanged,
      IEnumerable<SettingsPropertyGroupDefinition> settingPropertyGroups,
      IEnumerable<ISettingsPresetBuilder> presets)
    {
      this.Id = id;
      this.DisplayName = displayName;
      this.FolderName = folderName;
      this.SubFolder = subFolder;
      this.UIVersion = uiVersion;
      this.SubGroupDelimiter = subGroupDelimiter;
      this.SettingPropertyGroups = settingPropertyGroups.ToList<SettingsPropertyGroupDefinition>();
      this.Presets = presets.ToList<ISettingsPresetBuilder>();
      if (onPropertyChanged == null)
        return;
      this.PropertyChanged += onPropertyChanged;
    }

    public void Register()
    {
      foreach (IFluentPerSaveSettingsContainer settingsContainer in (IEnumerable<IFluentPerSaveSettingsContainer>) ((object) GenericServiceProvider.GetService<IEnumerable<IFluentPerSaveSettingsContainer>>() ?? (object) Array.Empty<IFluentPerSaveSettingsContainer>()))
        settingsContainer?.Register(this);
    }

    public void Unregister()
    {
      foreach (IFluentPerSaveSettingsContainer settingsContainer in (IEnumerable<IFluentPerSaveSettingsContainer>) ((object) GenericServiceProvider.GetService<IEnumerable<IFluentPerSaveSettingsContainer>>() ?? (object) Array.Empty<IFluentPerSaveSettingsContainer>()))
        settingsContainer?.Unregister(this);
    }

    public override BaseSettings CreateNew()
    {
      return (BaseSettings) new FluentPerSaveSettings(this.Id, this.DisplayName, this.FolderName, this.SubFolder, this.UIVersion, this.SubGroupDelimiter, (PropertyChangedEventHandler) null, this.SettingPropertyGroups.Select<SettingsPropertyGroupDefinition, SettingsPropertyGroupDefinition>((Func<SettingsPropertyGroupDefinition, SettingsPropertyGroupDefinition>) (g => g.Clone(false))), (IEnumerable<ISettingsPresetBuilder>) this.Presets);
    }

    public override sealed IEnumerable<ISettingsPreset> GetBuiltInPresets()
    {
      return this.Presets.Select<ISettingsPresetBuilder, ISettingsPreset>((Func<ISettingsPresetBuilder, ISettingsPreset>) (presetBuilder => presetBuilder.Build(this.CreateNew())));
    }
  }
}
